import collections


class Solution(object):
    def magnificentSets(self, n, edges):
        p = [-1] * n
        d = collections.defaultdict(set)
        for u, v in edges:
            u = u - 1
            v = v - 1
            d[u].add(v)
            d[v].add(u)
        st = []
        for i in range(n):
            if p[i] == -1:
                already = {i}
                now = {i}
                state = 0
                while now:
                    next_st = set()
                    for node in now:
                        p[node] = state
                        for v in d[node]:
                            if p[v] == -1:
                                next_st.add(v)
                                already.add(v)
                            else:
                                if p[v] == state:
                                    return -1
                    now = next_st
                    state = 1 - state
                st.append(already)
        ans = 0
        for t_list in st:
            max_val = 0
            for i in t_list:
                dist = 0
                now = {i}
                already = {i}
                while now:
                    dist += 1
                    next_st = set()
                    for node in now:
                        for v in d[node]:
                            if v not in already:
                                next_st.add(v)
                                already.add(v)
                    now = next_st
                max_val = max(max_val, dist)
            ans += max_val
        return ans


data = Solution()
n = 92
edges = [[67, 29], [13, 29], [77, 29], [36, 29], [82, 29], [54, 29], [57, 29], [53, 29], [68, 29], [26, 29], [21, 29],
         [46, 29], [41, 29], [45, 29], [56, 29], [88, 29], [2, 29], [7, 29], [5, 29], [16, 29], [37, 29], [50, 29],
         [79, 29], [91, 29], [48, 29], [87, 29], [25, 29], [80, 29], [71, 29], [9, 29], [78, 29], [33, 29], [4, 29],
         [44, 29], [72, 29], [65, 29], [61, 29]]

print(data.magnificentSets(n, edges))
